Socket
Socket
Sign inDemoInstall

express-async-handler

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-async-handler

Express Error Handler for Async Functions


Version published
Weekly downloads
162K
increased by4%
Maintainers
1
Weekly downloads
 
Created

What is express-async-handler?

The express-async-handler package is a middleware for Express.js that simplifies error handling in asynchronous route handlers. It allows you to write asynchronous code without having to manually catch errors and pass them to the next middleware.

What are express-async-handler's main functionalities?

Simplified Async Error Handling

This feature allows you to wrap your asynchronous route handlers with the asyncHandler function. If an error occurs, it will automatically be passed to the next middleware, simplifying error handling.

const express = require('express');
const asyncHandler = require('express-async-handler');

const app = express();

app.get('/', asyncHandler(async (req, res, next) => {
  const data = await someAsyncFunction();
  res.send(data);
}));

app.use((err, req, res, next) => {
  res.status(500).send({ error: err.message });
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Integration with Existing Middleware

This feature demonstrates how express-async-handler can be integrated with existing middleware like body parsers. It ensures that any errors in the asynchronous route handlers are properly caught and handled.

const express = require('express');
const asyncHandler = require('express-async-handler');

const app = express();

app.use(express.json());

app.post('/data', asyncHandler(async (req, res, next) => {
  const data = await saveData(req.body);
  res.status(201).send(data);
}));

app.use((err, req, res, next) => {
  res.status(500).send({ error: err.message });
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Other packages similar to express-async-handler

Keywords

FAQs

Package last updated on 15 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc